package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分页查询
     * @return
     */
    @Override
    public PageResult getPageCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> p=categoryMapper.pagelist(categoryPageQueryDTO);
        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(0);
      /*  category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.insertCategory(category);

    }

    /**
     * 分类信息修改
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
       /* category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/

        categoryMapper.update(category);

    }

    /**
     * 根据分类查询
     * @param type
     * @return
     */
    @Override
    public Category[] listByCategory(Integer type){

        Category[] arr=categoryMapper.listByCategory(type);
        return arr;
    }

    /**
     * 状态启用
     * @param status
     * @param id
     */
    @Override
    public void startOrstop(Integer status, Long id) {
        Category category=new Category();
        category.setId(id);
        category.setStatus(status);
       /* category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.updateStatus(category);
        //categoryMapper
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteById(id);
    }
}
